<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <HelloWorld msg="Welcome to Your Vue.js App"/>
    <input type="text" ref="username" v-model="username">

    <son-component ref="sonComponent"></son-component>

    <input type="file" name="avatar" id="avatar" ref="avatar">
    <button @click="getSonComponents">获取子组件属性</button>

    <div v-if="showEdit" class="title sr">
      <span>标题</span>
      <button @click="changeEdit(false)">编辑</button>
    </div>
    <div v-else class="alert sr">
      <input type="text" name="title" ref="title">
      <button @click="changeEdit(true)">确认</button>
    </div>
  </div>
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'
import SonComponent from './components/SonComponent.vue'

export default {
  name: 'App',
  components: {
    HelloWorld, SonComponent
  },
  data() { 
    return {
      username: '',
      showEdit: true
    }
  },
  mounted() {
    console.log(this.$refs.username);
  },
  methods: {
    changeEdit(flag) {
      this.showEdit = flag
      if (!flag) {
        this.$nextTick(() => {
          this.$refs.title.focus()
          console.log(789)
        })
        console.log(123)
      }
    },
    getSonComponents() {
      const sonComponent = this.$refs.sonComponent
      console.log(sonComponent.getUsername())
      console.log(sonComponent.username)

      this.username = sonComponent.username

      // 获取file
      // const avatar = this.$refs.avatar
      // console.log(avatar.files);
    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
.alert input {
  vertical-align: middle;
}

.sr button{
  padding: 5px 10px;
}
</style>
